package avicit.bdp.dgs.qa.rest;

import avicit.bdp.dds.dispatch.enums.ReleaseState;
import avicit.bdp.dgs.qa.dto.SchedulerDTO;
import avicit.bdp.dgs.qa.service.SchedulerService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-03-30
 * @类说明：SchedulerRest
 * @修改记录：
 * @注意事项：
 * @主要功能：质量作业调度Rest接口
 */
@RestController
@Api(tags = "qa", description = "质量作业调度Rest接口")
@RequestMapping("/api/bdp/dgs/qa/scheduler")
public class SchedulerRest {
    @Autowired
    private SchedulerService schedulerService;

    /**
     * 新增/修改调度配置
     * 注意：根据jobId确定是新增还是修改
     * @param schedulerDTO
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增调度配置")
    public ResponseMsg<Boolean> saveScheduler(@RequestBody SchedulerDTO schedulerDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.schedulerService.saveScheduler(schedulerDTO));

        return responseMsg;
    }

    /**
     * 查看调度配置详情
     * @param jobId
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查看调度配置详情")
    public ResponseMsg<Map<String, Object>> getScheduleDetail(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(schedulerService.getScheduleDetail(jobId));

        return responseMsg;
    }

    /**
     * 删除调度配置
     * @param jobId
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "查看调度配置详情")
    public ResponseMsg<Map<String, Object>> deleteSchedule(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        schedulerService.deleteSchedulerByJobId(jobId);

        return responseMsg;
    }

    /**
     * 启用定时配置
     * @param projectId
     * @param id
     * @return
     */
    @ApiOperation(value = "上线调度配置")
    @GetMapping("/online")
    public ResponseMsg<Map<String, Object>> online(@RequestParam(value = "projectId", required = false) String projectId,
                                                   @RequestParam("id") String id) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(schedulerService.setScheduleState(projectId, id, ReleaseState.ONLINE));

        return responseMsg;
    }

    /**
     * 停用定时配置
     * @param projectId
     * @param id
     * @return
     */
    @ApiOperation(value = "下线调度配置")
    @GetMapping("/offline")
    public ResponseMsg<Map<String, Object>> offline(@RequestParam(value = "projectId", required = false) String projectId,
                                                    @RequestParam("id") String id) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(schedulerService.setScheduleState(projectId, id, ReleaseState.OFFLINE));

        return responseMsg;
    }
}
